/*
 * Copyright (C) 2011 Fhpt All Rights Reserved.
 * 
 * MoCtrl.java
 */
package com.pay.sms;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.ui.Model;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * [文件名称]<br>
 * MoCtrl <br>
 * <br>
 * [文件描述]<br>
 * 内容摘要.<br>
 * <br>
 * [修改记录]<br>
 * 2012-4-13 ver1.00 创建 Administrator<br>
 * 
 * @author Administrator
 * @version 1.00
 */
public abstract class MoCtrl {
	
	public Logger logger = Logger.getLogger(MoCtrl.class);
	/**
	 * 错误编码
	 */
	protected String errorCode = null;

	/**
	 * 错误消息
	 */
	protected String errorMessage = null;
	
	
	
	protected void pageModel(Model model,Integer page,Object pageList,String title)throws IOException
	{
		model.addAttribute("title", title);
		model.addAttribute("data", pageList);
		if (null == page) {
			page = 1;
		}
		model.addAttribute("page", page);
	}
	
	protected void writeJson(HttpServletResponse response, String message) throws IOException{
		response.setContentType("application/json");
		response.getWriter().write(message);
	}
	
	protected void writeHtml(HttpServletResponse response, String message) throws IOException {
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write(message);
	}
	
	/**
	 * 将对象写成json流
	 * @param response  返回
	 * @throws IOException
	 */
	protected void writeObject(HttpServletResponse response, Object object) throws IOException{
		response.setContentType("application/json");
		SerializeWriter out = new SerializeWriter();
		JSONSerializer serializer = new JSONSerializer(out);
		serializer.write(object);
		out.writeTo(response.getWriter());
	} 

}
